﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Swin_e_mon_Forms
{
    public class CommandProcessor
    {
        private List<Command> commands = new List<Command>();

        public string Run(Player p, string input)
        {
            string[] inputSplit = input.Split(' ');
            
            if (input.Contains("and"))
            {
                string theCommands = "";
                List<String> tempInput = new List<String>();
                for (int i = 0; i < inputSplit.Length; i++)
                {
                    if (inputSplit[i].ToLower() != "and")
                        tempInput.Add(inputSplit[i]);
                    if (inputSplit[i].ToLower() == "and" || i == inputSplit.Length-1)
                    {
                        theCommands += CheckCommand(p, input, tempInput.ToArray()) + "\n\n";
                        tempInput = new List<String>();
                    }
                }
                return theCommands;
            }

            return CheckCommand(p, input, inputSplit);  
        }

        public string CheckCommand(Player p,string input, string[] inputSplit)
        {
            foreach (Command cmd in commands)
            {
                if (cmd.IdentifiedBy(inputSplit[0]) || cmd.IdentifiedBy(input))
                    return cmd.Run(p, inputSplit);
            }
            return "What was that?";
        }


        public void AddCommand(Command c)
        {
            commands.Add(c);
        }
    }
}
